Skip to content

feat: let Binder read and write non-public bean classes - #27368

Open
totally-not-ai[bot] wants to merge 10 commits into
mainfrom
feat/binder-non-public-bean-classes
Open

totally-not-ai[bot] wants to merge 10 commits into
mainfrom
feat/binder-non-public-bean-classes

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #27367

missing API · flow-data · apps that bind Binder or Grid to a bean class or record that is not public

Binder failed with an IllegalAccessException when the bean class was not public, for example a data class nested privately in a form. Applications had to make such classes public only for data binding. Now Binder (and other users of the bean property set, such as Grid(Class)) read and write properties of non-public classes, and writeRecord() creates non-public records.

Risks:

  • ⚠️ Behaviour change: bindings that used to throw for non-public bean types now work. Nothing that worked before changes.
  • ✅ No application-facing API change (one new helper in the internal ReflectTools class), no security, serialization, threading or performance concerns; public beans are still called directly; each accessor call adds one cheap canAccess() check.

Context. The accessors are public, but reflection still denies the call when the declaring class is not public. Other places in Flow already make members of application classes accessible the same way.

  • Getter and setter calls check canAccess() first and make the accessor accessible only when needed, the same way writeRecord() does. Public beans are called directly, as before.
    • When the package is in a named module that is not opened to Vaadin, an IllegalArgumentException names the member, class and package and says how to fix it.
  • writeRecord() makes the record constructor accessible when Binder cannot call it directly, and fails with the same message in that case.
  • Both use a new ReflectTools.makeAccessible(Executable) helper (internal package), next to the existing accessible-constructor logic there.
  • Added tests with a non-public bean (including a nested property) and a non-public record, declared outside the binder package.

Chose this over the alternative of MethodHandles lookups, which need the application to pass in a lookup and would add public API.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Type of change

  • Feature

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the changes enabling access to non-public beans and records.

No open issues 🎉

What changed in this PR

Enables Binder and other bean-property consumers, such as Grid, to access non-public bean classes without requiring applications to expose those types publicly.

Changes:

  • Retries inaccessible getter and setter calls with reflective access enabled, with guidance when module restrictions prevent access.
  • Enables reflective construction of non-public records in writeRecord().
  • Adds regression tests for non-public beans, nested properties, and record round trips.
File Description
flow-data/​src/​test/​java/​com/​vaadin/​flow/​tests/​data/​bean/​NonPublicBeans.java Adds non-public bean and record fixtures outside the binder package.
flow-data/​src/​test/​java/​com/​vaadin/​flow/​data/​binder/​BinderTest.java Tests reading and writing a non-public record.
flow-data/​src/​test/​java/​com/​vaadin/​flow/​data/​binder/​BeanPropertySetTest.java Tests non-public bean accessors and nested properties.
flow-data/​src/​main/​java/​com/​vaadin/​flow/​data/​binder/​Binder.java Enables access to non-public record constructors.
flow-data/​src/​main/​java/​com/​vaadin/​flow/​data/​binder/​BeanPropertySet.java Retries inaccessible accessors and explains module-access failures.

Review effort: Balanced

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 651 files   - 4   1 742 suites   - 4   1h 41m 39s ⏱️ - 5m 23s
14 077 tests ±0  14 006 ✅ +1  71 💤  - 1  0 ❌ ±0 
14 503 runs  ±0  14 432 ✅ +1  71 💤  - 1  0 ❌ ±0 

Results for commit 5746141. ± Comparison against base commit 3c06b37.

♻️ This comment has been updated with latest results.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Fixed the nested try (java:S1141): the fallback that makes the accessor accessible is now its own method. I also added a test for the case where the package isn't opened, so the error message branch is now covered. I left the other findings (final fields and the generic type name in Binder/BeanPropertySet) alone. They are on existing lines this PR doesn't touch, so they belong in a separate cleanup.

@Artur-
Artur- marked this pull request as ready for review December 2, 2026 06:32

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changes — left 2 comments.

Finding
📐 The new test asserts the exact wording of the exception message, against the testing convention.
📐 The new reflection helper is kept private in BeanPropertySet although ReflectTools already owns this pattern.


IllegalStateException exception = assertThrows(
IllegalStateException.class, () -> getter.apply(bean));
assertEquals(String.format(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 The new test asserts the exact wording of the exception message, against the testing convention.

CONVENTIONS.md (Testing): "When a test checks an exception or log message … assert that it contains the parts that matter, such as the class name and the offending value, not the exact wording." Assert that the message contains the class name, the package name and the method name instead.

The case also leans on a JDK implementation detail (sun.nio.fs being neither exported nor opened), so pinning the full wording makes it doubly brittle.

BeanPropertySetTest.java:588 · conventions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vaadin-review-bot The test no longer pins the full wording. It now checks that the message names the method, the class, the package that is not opened, and points to module-info.java. It also expects IllegalArgumentException now, since the helper moved to ReflectTools (see the other thread). The exact message is checked in ReflectToolsTest against the constant, like the existing accessible-constructor test there.

// S3011: bean types do not need to be public, e.g. a data class nested
// in a view, so their public accessors are not accessible as such
@SuppressWarnings("java:S3011")
private static void makeAccessible(Method method,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 The new reflection helper is kept private in BeanPropertySet although ReflectTools already owns this pattern.

CONVENTIONS.md (Public API): "A method that does not touch the state of the class it sits on belongs elsewhere … Search for an existing utility before writing a helper, and when the helper is genuinely new, put it in the util class it belongs to (ReflectTools, FrontendUtils, …) instead of keeping it private where it is needed first."

ReflectTools already carries the same "Its module does not open the package '%s' to Vaadin" wording in CREATE_INSTANCE_FAILED_INACCESSIBLE_CONSTRUCTOR and the same trySetAccessible guard in createInstanceWithAccessibleConstructor. Moving the helper there would also let writeRecord reuse it.

BeanPropertySet.java:570 · conventions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vaadin-review-bot Moved it to ReflectTools.makeAccessible(Executable), next to createInstanceWithAccessibleConstructor. It throws IllegalArgumentException like the other helpers there. BeanPropertySet calls it when a getter or setter call is denied, and writeRecord() now uses it too, but only when Binder cannot call the record constructor directly, so a record in a package exported only to Vaadin keeps working. A record in a package that is not opened now gets the descriptive message instead of the generic access error. I kept the existing constructor message separate: it gives instance-creation advice ("its no-arg constructor public") that does not fit getters, setters or record constructors.

writeRecord() now uses the same helper, so a record in a package that
is not opened to Vaadin fails with the descriptive message instead of
a generic access error. The tests assert the parts of the message
that matter instead of its exact wording.
A public record whose package is exported only to Vaadin can be
created directly, while trySetAccessible() fails for it. Check access
first so such records keep working. Tests now also check the package
and the fix-it hint in the message. Only the helper is tested for the
package-not-open case; no JDK record in such a package fits a Binder
test.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

Getters and setters now check canAccess() and make the method
accessible before the call, like writeRecord() does, instead of
catching the IllegalAccessException and retrying. Also fixes the
operator precedence and single-throwing-call lambda findings.
@totally-not-ai

totally-not-ai Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the three SonarCloud findings:

  • ReflectTools.makeAccessible: added parentheses around the instanceof check in the ternary.
  • ReflectToolsTest: the constructor is looked up before the lambda, so only makeAccessible runs inside assertThrows.
  • BeanPropertySet: the getter/setter call no longer catches the IllegalAccessException and retries. It checks canAccess() first and makes the method accessible when needed, like writeRecord() does. A null bean still gets the NullPointerException from invoke(), as before.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

@totally-not-ai

totally-not-ai Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the remaining operator-precedence finding in ReflectTools.makeAccessible: the flagged part was the string concatenation in the ternary's else branch, so the quoted method name is now built with String.format instead. No behaviour change.

@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let Binder read non-public classes

3 participants